package com.victor9ac.tfm.rome2rio.api.connector;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.jdom.Document;

import com.victor9ac.tfm.mashup.uris.ConnectionBean;
import com.victor9ac.tfm.mashup.uris.MashupURIFactory;

public class Rome2RioConnector 
//extends MashupConnectorRestTemplate 
{
	
	private String apiKey;
	String origin;
	XMLResponseHandler xmlResponseHandler;	
	ConnectionBean connectionBean;
	
	
	Map<String, String> urlParameters;

	
	ResourceBundle rb = ResourceBundle.getBundle("auth.rome2rio-auth");
	
	public Rome2RioConnector(){
		super();
		this.apiKey = rb.getString("apiKey");
		this.urlParameters = new HashMap<String, String>();
		this.urlParameters.put("key", this.apiKey);
	}
	
	public Rome2RioConnector(String apiKey){
		super();
	
		this.apiKey = apiKey;
		this.urlParameters = new HashMap<String, String>();
		this.urlParameters.put("key", this.apiKey);
		
	}

	public Document execute (String dest) throws Exception{
		Document doc = null;
		
		urlParameters.put("oName",origin);
		urlParameters.put("dName", dest);
		
		doc = executeHttpQuery(doc);
		
		return doc;
	}

	
	public Document execute (String dest, String origin) throws Exception{
		Document doc = null;

		urlParameters.put("oName", origin);
		urlParameters.put("dName", dest);
		
		doc = executeHttpQuery(doc);
		
		return doc;
	}
	protected URI getUri(String params) {
		return new MashupURIFactory(this.connectionBean, null, params).getUri();
	}

	private Document executeHttpQuery(Document doc) throws Exception {
		String params = buildChainParameters();
		
		try {
			HttpClientBuilder create = HttpClientBuilder.create();
			HttpClient client = create.build();

			
			URI uri = getUri(params);
			HttpGet request = new HttpGet(uri);		
			HttpResponse response = client.execute(request);
			
			
			System.out.println("Response Code : " 
	                + response.getStatusLine().getStatusCode());

			doc = xmlResponseHandler.handleResponse(response);

			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new Exception(" error conectando con rome2rio ...");
		}
		return doc;
	}

	private String buildChainParameters() {
		return "?"+"key="+ urlParameters.get("key")+"&"+"oName="+urlParameters.get("oName")+"&"+"dName="+urlParameters.get("dName");
	}

	public void setXmlResponseHandler(XMLResponseHandler xmlResponseHandler) {
		this.xmlResponseHandler = xmlResponseHandler;
	}

	public String getApiKey() {
		return apiKey;
	}

	public void setApiKey(String apiKey) {
		this.apiKey = apiKey;
	}

	public String setOrigin(String defaultLocation) {
		if (defaultLocation != null){
			this.origin = defaultLocation;
		}else{
			
			System.out.println("requesing travel from defaultLocation:"+this.origin);
		}
		return this.origin;
		
	}

	public void setConnectionBean(ConnectionBean connectionBean) {
		this.connectionBean = connectionBean;
	}

	public String getOrigin() {
		return origin;
	}


	

}
